package beanstao.util.store.database;

import java.util.HashMap;

import org.dom4j.Element;

import beanstao.util.enums.DataType;
import beanstao.util.exception.technical.dal.domain.DomainSetException;
import beanstao.util.log.LogHelper;
import beanstao.util.xml.XDocument;

/**
 * 表示一个数据库表中的字段的相关属性
 * @author beanstao@google.com 
 */
public class Field
{
	private static final String DATA_TYPE = "dataType";
	private static final String DEFAULTVALUE = "defaultValue";
	private static final String INDEX = "index";
	/**
	 * 数据库的字段的值"不能为Null"时的表达方式
	 */
	private static final String NOT_NULL = "notNull";
	private static final String LENGTH = "length";
	private static final String NAME = "name";
	private static final String PRIMARY = "primary";
	private static final String AUTOINC = "autoIncrement";

	public static Field fromGeneralElement(final Element element)
	{
		HashMap<String, String> map = XDocument.toMapByAttributes(element);
		Field field = new Field();
		if (map.containsKey(NAME))
			field._name = map.get(NAME);
		else
			throw new DomainSetException();
		if (map.containsKey(DATA_TYPE))
			try
			{
				field._DataType = Enum.valueOf(DataType.class, map.get(DATA_TYPE).toUpperCase());
			}
			catch (Exception e)
			{
				LogHelper.logWarn(field, "EnumType parse error. " + map.get(DATA_TYPE).toUpperCase());
			}
		if (map.containsKey(LENGTH))
			field._length = Integer.parseInt(map.get(LENGTH));
		field._NotNull = isMe(map, NOT_NULL);
		field._isPrimary = isMe(map, PRIMARY);
		field._isAutoIncrement = isMe(map, AUTOINC);

		if (map.containsKey(DEFAULTVALUE))
			field._Default = map.get("defaultValue");
		if (map.containsKey(INDEX))
			field._Index = map.get("index");

		return field;
	}

	/**
	 * 获取该字段是否可以为Null。false时该字段可以为Null。
	 */
	private boolean _NotNull = false;
	private DataType _DataType = DataType.VARCHAR;
	private Object _Index = 0;
	private Object _Default = 0;
	private int _length = 8;
	private String _name = "";
	private boolean _isPrimary = false;
	private boolean _isAutoIncrement = false;

	public DataType getDataType()
	{
		return _DataType;
	}

	public void setDataType(final DataType dataType)
	{
		_DataType = dataType;
	}

	public Object getIndex()
	{
		return _Index;
	}

	public void setIndex(final Object index)
	{
		_Index = index;
	}

	/**
	 * 获取该字段是否可以为Null
	 * @return false时该字段可以为Null。
	 */
	public String getNullEnable()
	{
		return _NotNull ? "NOT NULL" : "";
	}

	public void setNullEnable(final boolean nullEnable)
	{
		_NotNull = nullEnable;
	}

	public Object getDefault()
	{
		return _Default;
	}

	public void setDefault(final Object defaultValue)
	{
		_Default = defaultValue;
	}

	public int getLength()
	{
		return _length;
	}

	public void setLength(final Integer length)
	{
		_length = length;
	}

	public String getName()
	{
		return _name;
	}

	public void setName(final String name)
	{
		_name = name;
	}

	public static boolean isMe(HashMap<String, String> map, String me)
	{
		if (map.containsKey(me))
		{
			String value = map.get(me).toLowerCase();
			if (value.equals("true") || value.equals("1"))
				return true;
		}
		return false;
	}

	public boolean isPrimary()
	{
		return _isPrimary;
	}

	public boolean isAutoIncrement()
	{
		return _isAutoIncrement;
	}

	@Override
	public String toString()
	{
		StringBuilder builder = new StringBuilder();
		builder.append("Field [_DataType=");
		builder.append(_DataType).append(", _Default=").append(_Default).append(", _Index=").append(_Index).append(", _NotNull=").append(_NotNull).append(", _length=").append(_length).append(", _name=").append(_name).append("]");
		return builder.toString();
	}

}
